package com.evil.application.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.evil.application.pojo.dto.template.print.PrintTemplateListReqDTO;
import com.evil.application.pojo.entity.ApplicationPrintTemplate;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.core.exception.BusinessException;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
public interface ApplicationPrintTemplateMapper extends BaseMapper<ApplicationPrintTemplate> {

    default Optional<ApplicationPrintTemplate> isExistById(Long id) {
        return Optional.ofNullable(this.selectById(id));
    }

    default ApplicationPrintTemplate findById(Long id) {
        return this.isExistById(id).map(e -> {
            if (SwitchEnum.YES.getId() == e.getIsDeleted()) {
                throw new BusinessException(RCodeEnum.APPLICATION_PRINT_TEMPLATE_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_PRINT_TEMPLATE_NOT_EXIST));
    }

    Optional<ApplicationPrintTemplate> isExistByKey(String key);

    default ApplicationPrintTemplate findByKey(String key) {
        return this.isExistByKey(key).map(e -> {
            if (SwitchEnum.YES.getId() == e.getIsDeleted()) {
                throw new BusinessException(RCodeEnum.APPLICATION_PRINT_TEMPLATE_DELETED);
            }
            return e;
        }).orElseThrow(() -> new BusinessException(RCodeEnum.APPLICATION_PRINT_TEMPLATE_NOT_EXIST));
    }

    List<ApplicationPrintTemplate> findByIds(@Param("ids") Collection<Long> ids);

    List<ApplicationPrintTemplate> findByEnterpriseId(Long enterpriseId);

    List<ApplicationPrintTemplate> findByAppId(Long applicationId);

    List<ApplicationPrintTemplate> printTemplateList(PrintTemplateListReqDTO printTemplateListReqDTO);
}